package com.ruoyi.common.tools.excel;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;

import com.ruoyi.common.tools.excel.base.MyExcelImportUtil;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.tools.excel.base.MyExcelResult;
import com.ruoyi.common.tools.utils.SerialGenerator;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.oss.core.OssClient;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyExcelUtil {

	/**
	 * excel文件从oss下载解析
	 * 
	 * @param bo
	 * @param batchNo
	 * @return
	 */
	public static <T> MyExcelResult<T> getImportExcelData(final String batchNo, final String ossId,
			MyExcelListener<T> excelListener, Class<T> clazz) {

		return getImportExcelData(batchNo, ossId, excelListener, clazz, false);
	}

	/**
	 * excel文件从oss下载解析
	 * 
	 * @param bo
	 * @param batchNo
	 * @return
	 */
	public static <T> MyExcelResult<T> getImportExcelData(final String batchNo, final String ossId,
			MyExcelListener<T> excelListener, Class<T> clazz, boolean readPic) {
		MyExcelResult<T> excelResult = null;

		SysOssVo oss = SpringUtils.getBean(ISysOssService.class).getById(NumberUtils.toLong(ossId));
		if (null == oss) {
			return null;
		}
		try {
			OssClient storage = OssFactory.instance();
			InputStream inputStream = storage.getObjectContent(oss.getUrl());
			byte[] buffer = IoUtil.readBytes(inputStream);
			ByteArrayInputStream bufIs = new ByteArrayInputStream(buffer);

			if (readPic) {
				Map<Integer, Map<Integer, String>> picMap = getPictures(bufIs);
				excelListener.setExcelPicMap(picMap);
				bufIs.reset();
			}

			excelResult = MyExcelImportUtil.importExcel(bufIs, clazz, excelListener);

			inputStream.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return excelResult;
	}

	/**
	 * 获取图片和位置 (xlsx)
	 * 
	 * @param sheet
	 * @return
	 * @throws IOException
	 */
	public static Map<Integer, Map<Integer, String>> getPictures(InputStream excelInput) throws IOException {

		XSSFWorkbook wb = new XSSFWorkbook(excelInput);
		final XSSFSheet sheet = wb.getSheetAt(0);// 得到Excel工作表对象

		ISysOssService ossService = SpringUtils.getBean(ISysOssService.class);

		Map<Integer, Map<Integer, String>> map = new HashMap<>();
		List<POIXMLDocumentPart> list = sheet.getRelations();
		for (POIXMLDocumentPart part : list) {
			if (part instanceof XSSFDrawing) {
				XSSFDrawing drawing = (XSSFDrawing) part;
				List<XSSFShape> shapes = drawing.getShapes();
				// int col = 0;
				// int row = 0;
				for (XSSFShape shape : shapes) {
					XSSFPicture picture = (XSSFPicture) shape;
					XSSFClientAnchor anchor = picture.getPreferredSize();
					CTMarker marker = anchor.getFrom();
					Integer rowKey = marker.getRow();
					Integer colKey = marker.getCol();
					Map<Integer, String> picMap = map.get(rowKey);
					if (null == picMap) {
						picMap = new HashMap<>();
						map.put(rowKey, picMap);
					}
					String fileName = SerialGenerator.generator.nextId() + '.'
							+ picture.getPictureData().suggestFileExtension();
					String ossId = ossService.upload(fileName, picture.getPictureData().getData());
					picMap.put(colKey, ossId);
				}
			}
		}
		wb.cloneSheet(0);
		wb.close();
		return map;
	}
}
